package cn.tedu.lucky.draw.server.scheduler;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component
@EnableScheduling//用于启动基于注解的定时任务，spring的任务调度就会被激活
public class ActivityScheduler {
    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job activityJob;

    @Autowired
    private JobExplorer jobExplorer;


    //定义一个定时任务，通过注解来指定执行规律
    @Scheduled(cron = "0 0/1 * * * ?")//每分钟执行一次
    public void runJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer)
                .getNextJobParameters(activityJob).toJobParameters();
        jobLauncher.run(activityJob,jobParameters);
    }
}
